﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ObTookit.HighLight
{
    public class BaseHighLight
    {
        /// <summary>
        /// 关键字
        /// </summary>
        public string[] Keywords;
        /// <summary>
        /// 函数方法
        /// </summary>
        public string[] Functions;
        /// <summary>
        /// 字符串
        /// </summary>
        public string[] Strings;
        /// <summary>
        /// 空白
        /// </summary>
        public string[] WhiteSpace;
        /// <summary>
        /// 注释
        /// </summary>
        public string CommentTags;
        /// <summary>
        /// 特殊（比如#region)
        /// </summary>
        public string SpecilChars;

        public Color KeywordColor;
        public Color FunctionColor;
        public Color StringColor;
        public Color CommentColor;
        public Color SpecilCharColor;
        public Color BackgroundColor;
        public Color DefaultColor;
        /// <summary>
        /// 高亮代码（只针对只读小文本）
        /// </summary>
        /// <param name="rtb"></param>
        /// <param name="rtbText"></param>
        /// <param name="wordList"></param>
        /// <param name="mainColor"></param>
        public void SetHighLight(System.Windows.Forms.RichTextBox rtb, string rtbText, string[] wordList, Color mainColor)
        {
            foreach (string word in wordList)
            {
                Regex reg = new Regex(word + "\\s|\\s" + word + "\\W"); //匹配关键字（前有空格或者后又空格）
                MatchCollection mc = reg.Matches(rtbText);
                foreach (Match m in mc)
                {
                    rtb.Select(m.Index, m.Length);
                    rtb.SelectionColor = mainColor;
                }
            }
        }

        public void SetHighLight(System.Windows.Forms.RichTextBox rtb, string rtbText, string[] wordList, Color mainColor, CodeType ct)
        {
            if (ct == CodeType.KEYWORD)
            {
                foreach (string word in wordList)
                {
                    Regex reg = new Regex(word + "\\s|\\s" + word); //匹配关键字（前有空格或者后又空格）
                    MatchCollection mc = reg.Matches(rtbText);
                    foreach (Match m in mc)
                    {
                        rtb.Select(m.Index, m.Length);
                        rtb.SelectionColor = mainColor;
                    }
                }
            }
            else if (ct == CodeType.COMMENT)
            {
                foreach (string word in wordList)
                {
                    Regex reg = new Regex(word + ".*"); //匹配关键字（^/.*）
                    MatchCollection mc = reg.Matches(rtbText);
                    foreach (Match m in mc)
                    {
                        rtb.Select(m.Index, m.Length);
                        rtb.SelectionColor = mainColor;
                    }
                }
            }
        }

        public enum CodeType
        {
            KEYWORD,
            COMMENT,
            STRING,
        }
    }
}
